class Observer {
    constructor(data) {
        this.walk(data);
    }
    walk(data) {
        if (!data || typeof data !== "object") return;
        Object.keys(data).forEach(key => {
            this.defineRective(data, key, data[key]);
        });
    }
    defineRective(data, key, val) {
        let self = this
        this.walk(data[key]);

        let dep = new Dep()

        Object.defineProperty(data, key, {
            enumerable: true,
            configurable: true,
            get() {
                console.log('get');
                Dep.target && dep.addSub(Dep.target)
                return val;
            },
            set(newVal) {
                console.log('set');
                if (val === newVal) return;
                val = newVal;
                // 当改变的val是对象时 需要给它下面的属性加上劫持
                self.walk(newVal)
                dep.notify()
            },
        });
    }
}
